#ifndef ROOM_H
#define ROOM_H

#include "core/global.h"

#include <QTcpServer>
#include <QPoint>

class ServerPlayer;

class Room : public QTcpServer
{
    Q_OBJECT
    Q_PROPERTY(QString phase READ getPhaseString)

public:
    enum Phase{Waiting, Playing};

    explicit Room(QObject *parent = 0);

    QString getRoomName() const;
    void setRoomName(const QString &name);

    Phase getPhase() const;
    QString getPhaseString() const;

    int getMaxPlayerNum() const;
    void setMaxPlayerNum(int num);

    void startGame();
    void endGame();

    void activateCurrentPlayer();
    void deactivateCurrentPlayer();
    void nextPlayer();
    void activateNextPlayer();

    void sendChatMessage(ServerPlayer *player, const QString &message);
    void sendSystemMessage(const QString &message);
    void sendMovePieceAction(int player_id, int from, int to);
    void sendPlayerProperty(ServerPlayer *player, const QString &p_key, const QString &p_value);

    void broadcast(CommandType command, const QString &data);
    void broadcast(CommandType command, const QByteArray &data);

public slots:
    void setConfig(qint16 port, const QString &name, int max_player_num);
    void addReadyPlayer();

private:
    QString room_name;
    Phase phase;
    int max_player_num;
    QList<QTcpSocket *> player_sockets;
    QList<ServerPlayer *> players;
    int ready_player_num;

    ServerPlayer *current_player;

protected slots:
    void addNewPlayer();

signals:
    void newPlayerName(int player_id, QString name);
    void serverNameChanged(QString);
    void playerNumChanged(int);
    void serverPortChanged(ushort);

    void occurError(QString);
};

#endif // ROOM_H
